#include "widget.h"
#include "ui_widget.h"

#include <QCheckBox>
#include <QLineEdit>
#include <QListWidget>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->tableWidget->setColumnCount(5);//设置列数为5
    ui->tableWidget->setRowCount(1);//设置列数为5
    QStringList header;
    header << QStringLiteral("文件") << QStringLiteral("大小")<<QStringLiteral("创建时间")<<QStringLiteral("修改时间")<<QStringLiteral("访问时间");
    ui->tableWidget->setHorizontalHeaderLabels(header);

    QTableWidgetItem *TablestrTime = new QTableWidgetItem("2023");
    TablestrTime->setTextAlignment(Qt::AlignCenter);
    QTableWidgetItem *Tablestr = new QTableWidgetItem("23");
    Tablestr->setTextAlignment(Qt::AlignCenter);

    ui->tableWidget->setItem(0,0,new QTableWidgetItem("System"));
    ui->tableWidget->setItem(0,1,Tablestr);//文本居中
    ui->tableWidget->setItem(0,2,TablestrTime);
    ui->tableWidget->setItem(0,3,new QTableWidgetItem("3434"));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    EditDialog *editDialog = new EditDialog;
    connect(editDialog, &EditDialog::doneEdit, this, &Widget::editTable);
    editDialog->show();
}

void Widget::editTable(QStringList items, int type)
{
    QTableWidgetItem *TablestrTime = new QTableWidgetItem("2023");
    TablestrTime->setTextAlignment(Qt::AlignCenter);
    QTableWidgetItem *Tablestr = new QTableWidgetItem("23");
    Tablestr->setTextAlignment(Qt::AlignCenter);

    int index = 0;
    int row = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(row);
    foreach(auto text, items) {
        //qDebug()<<text;
        ui->tableWidget->setItem(row,index++,new QTableWidgetItem("System"));
    }
    // 复选框
    this->TableWidgetAddCheckBox(ui->tableWidget, row, index++, NULL, Qt::Unchecked);
    // 下拉框
    QStringList itemss;
    //静止超时,在TableWidget的(0,1)单元格设置一个下拉框，并设置初始下标为3![请添加图片描述](https://img-blog.csdnimg.cn/a87bcc562ffd483391ef266ee7812df2.png)
    itemss << tr("关闭") << tr("60秒") << tr("120秒") << tr("180秒") << tr("240秒") << tr("300秒");
    Widget::TableWidgetAddComboBox(ui->tableWidget,row,index,itemss,3);
    itemss.clear();
}

void Widget::on_tableWidget_cellClicked(int row, int column)
{
    // QTableWidgetItem *item = ui->tableWidget->currentItem();
    // ui->textEdit->append(item->text());
    ui->textEdit->append(ui->tableWidget->item(row, 0)->text() + "-" + ui->tableWidget->item(row, 1)->text());
}

void Widget::on_tableWidget_itemClicked(QTableWidgetItem *item)
{

}

void Widget::comboxIndexChange(int index)
{
    qDebug() << index;
}

//TableWidget中加入复选框
void Widget::TableWidgetAddCheckBox(QTableWidget *tableWidget, int x, int y, QString text, Qt::CheckState checkState)
{
    QWidget *widget = new QWidget();
    QHBoxLayout *layout = new QHBoxLayout();
    QCheckBox *checkBox = new QCheckBox;

    checkBox->setText(text);                		//复选框文本
    checkBox->setCheckState(checkState);   			//复选框初始状态
    layout->addWidget(checkBox,0,Qt::AlignCenter);	//居中
    layout->setMargin(0);							//左右间距
    widget->setLayout(layout);
    tableWidget->setCellWidget(x,y,widget);
}

//设置comboBox的下拉内容，并设置内容居中
void Widget::ComboBoxSetItems(QComboBox *comboBox, QStringList *items)
{
    QListWidget *listWidget = new QListWidget(this);
    QLineEdit *lineEdit = new QLineEdit;

    for(int i=0;i<items->count();i++)
    {
        QListWidgetItem *item = new QListWidgetItem(items->at(i));
        item->setTextAlignment(Qt::AlignCenter);
        listWidget->addItem(item);
    }

    //ComboBox使用listWidget的内容
    comboBox->setModel(listWidget->model());
    comboBox->setView(listWidget);

    lineEdit->setReadOnly(true);
    lineEdit->setAlignment(Qt::AlignCenter);

    comboBox->setLineEdit(lineEdit);
}

//TableWidget中加入下拉框,并设置下拉框的内容及初始下标
void Widget::TableWidgetAddComboBox(QTableWidget *tableWidget,int x, int y, QStringList items,int comboBoxIdx)
{
    QComboBox *combox = new QComboBox();
    combox->setStyleSheet("background-color:rgb(255,255,255)");
    Widget::ComboBoxSetItems(combox,&items);		//下拉框的内容
    combox->setCurrentIndex(comboBoxIdx);				//下拉框初始下标
    tableWidget->setCellWidget(x,y,(QWidget*)combox);

    //绑定ComboBox值改变信号
    connect(combox, SIGNAL(currentIndexChanged(int)), this, SLOT(comboxIndexChange(int)));

}


